package com.nettyRpc.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class MyServerHandler extends SimpleChannelInboundHandler<String> {




	@Override
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
		//System.out.println(ctx.channel().remoteAddress()+" , "+ msg );
		//ChannelFuture cf =ctx.channel().writeAndFlush("from server : "+UUID.randomUUID());
		ctx.channel().writeAndFlush("is ok \r\n");
		//ctx.channel().close();
	}
	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		if(evt instanceof IdleStateEvent) {
			IdleStateEvent event =(IdleStateEvent)evt;
			if(event.state().equals(IdleState.READER_IDLE)) {
				System.out.println("---读空闲--");
				ctx.channel().close();
			}else if(event.state().equals(IdleState.WRITER_IDLE)) {
				System.out.println("---写空闲--");

			}else if(event.state().equals(IdleState.ALL_IDLE)) {
				System.out.println("---读写空闲--");
				ctx.channel().writeAndFlush("ping  \r\n");

			}
		}
		super.userEventTriggered(ctx, evt);
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		//System.out.println("---------channelActive");
		//super.channelActive(ctx);
	}



	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		ctx.close();
	}
}
